<?php
namespace app\index\model;
use think\Model;

class Register extends Model
{
    /*用户注册*/
    public function index(){
         $data['username'] = trim(input('post.username'));
        //判断数据库该用户名是否已经注册
        if(db('users')->field('username')->where($data)->count()){
           //用户已经存在
            return 602;
        }else{    
            $data['password'] = trim(md5(md5(input('post.password'))));
            $agree = input('post.agree');
            $code = trim(input('post.code'));
            $msg = db('users')->field('uid,icode1,icode2')->where('icode1',$code)->whereOr('icode2',$code)->find();
            $data['pid'] = $msg['uid'];
            if($code == $msg['icode1']){
                //用户填写邀请码1成为会员
                $data['vip'] = 1;
                $data['createtime'] = date('Y-m-d H:i:s');
                $data['ip'] = request()->ip();
                if(!$agree){
                        return 4;
                    }else{ 
                      $content = '添加用户管理中注册用户';     
                      db('users')->insertGetId($data);
                      $msg['icode1'] = " ";
                      db('users')->where('uid',$data['pid'])->update($msg);
                      //成功返回注册的用户
                      return 1;
                    }
            }elseif($code == $msg['icode2']){
                $data['vip'] = 2;
                $data['createtime'] = date('Y-m-d H:i:s');
                $data['ip'] = request()->ip();
                if(!$agree){
                    return 4;
                }else{          
                  db('users')->insertGetId($data);
                  $msg['icode2'] = " ";
                  db('users')->where('uid',$data['pid'])->update($msg);
                  //成功返回注册的用户
                  return 1;
                }
            }else{
                //不存在该邀请码
                return 604;
            }
        }       
    }
}
